package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.Md5jarss;
import com.woke.method.MyApp;
import com.woke.uiviewpay.DialogWidget;
import com.woke.uiviewpay.PayPasswordView;
import com.woke.uiviewpay.PayPasswordView.OnPayListener;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Tixian1Activity extends Activity {
	
	private TextView mEbankname;
	private TextView mTok;
	private MyApp application;
	private TextView mFrame;
	private String cardType,list_bankname;
	private ArrayList<String> datalist = new ArrayList<String>();
	private String order_id,playform;
	private Intent intent;
	private ArrayList<String> lists;
	private DialogWidget mDialogWidget;//����֧������
	private EditText mEmoney;
	
	//���֣����֤�����ţ��绰���������
	
		private String name;//����
		private String sfzno;//���֤��
		private String carno;//���п���
		private String phone;//�ֻ��
		private String bankname;//�������
		
		private String money;//��ֵ���
		private TextView mTcarno;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tixian1);
		
		application = (MyApp) getApplication();
		intent = getIntent();
		lists = intent.getStringArrayListExtra("chongzhi1");
		name = lists.get(0);sfzno = lists.get(1);carno = lists.get(2);
		phone = lists.get(3);
		bankname = lists.get(4);
		intview();
	}
	
private void intview() {
		
		mEbankname = (TextView) findViewById(R.id.avtixian1_text_bankname);//����
		mEmoney = (EditText) findViewById(R.id.avtixian1_edit_money);
		mTcarno = (TextView) findViewById(R.id.avtixian1_text_cardno);
		mTok = (TextView) findViewById(R.id.avtixian1_text_tixian);
		mFrame = (TextView) findViewById(R.id.avtixian1_text_bankname);
//		findViewById(R.id.avtixian1_relatie_morebank).setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				intent.setClass(Tixian1Activity.this, MybankblindActivity.class);
//				startActivity(intent);
//			}
//		});
		findViewById(R.id.avczhi1_image_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mTok.setOnClickListener(new OnClickListener() {
			

			@Override
			public void onClick(View v) {
				if (application.getDatas_load ()==null)return;
				money = ""+ mEmoney.getText().toString().trim();
				if(null == application.getDatas_load().getPay_pwd() || application.getDatas_load().getPay_pwd().equals("")){
					Toast.makeText(Tixian1Activity.this, "请设置支付密码", Toast.LENGTH_SHORT).show();
				}else if(carno.equals("")){
					Toast.makeText(Tixian1Activity.this, "请输入提现金额", Toast.LENGTH_SHORT).show();
				}else{
					mDialogWidget=new DialogWidget(Tixian1Activity.this, getDecorViewDialog());
					mDialogWidget.show();
//					getpayid("nocard");
				}
//				else if(money.equals("")){
//					Toast.makeText(Chongzhi1Activity.this, "�������ֵ���", Toast.LENGTH_SHORT).show();
//				}else{
//				}
			}
		});
		
		findViewById(R.id.avczhi1_relatie_morebank).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				intent.setClass(Tixian1Activity.this, MybankblindActivity.class);
				startActivity(intent);
			}
		});
		//���֣����֤�����ţ��绰���������
		mEbankname.setText(bankname);
		mTcarno.setText("尾号"+carno.substring(carno.length()-4,carno.length()));
	}
	
protected View getDecorViewDialog() {
	// TODO Auto-generated method stub
	return PayPasswordView.getInstance(money,this,new OnPayListener() {
		
		@Override
		public void onSurePay(String password) {
			// TODO Auto-generated method stub
			mDialogWidget.dismiss();
			mDialogWidget=null;
			
			String get32md5 = Md5jarss.get32MD5("sp_");
			String get32md52 = Md5jarss.get32MD5(password);
			String sp1 = get32md5.substring(0, 12);
			String sp11 = get32md5.substring(get32md5.length()-4, get32md5.length());
			
			Log.e("sp的"+get32md5, "支付密碼"+get32md52);
			String pswold = sp1 +get32md52+sp11;
			if(application.getDatas_load ()!=null) {
				if (! pswold.equals (application.getDatas_load ().getPay_pwd ())) {
					Toast.makeText (Tixian1Activity.this, "支付密码不正确", Toast.LENGTH_SHORT).show ();
				} else {
					String getuser_money = application.getDatas_load ().getUser_money ();
					int paymoney = Integer.parseInt (money);
					int allmoney = Integer.parseInt (getuser_money);
//				float allmoney=Float.parseFloat(getuser_money);
//				float paymoney=Float.parseFloat(money);
					Log.e ("allmoney" + allmoney, "paymoney" + paymoney);
					if ((allmoney - paymoney) > 0) {
//					Log.e("flomoney"+flomoney*100, "Ǯ"+flomoney);
//					String lastmonsd = ""+flomoney;
//					lastmonsd.substring(0, lastmonsd.length()-2);
						tixiandata ("" + paymoney * 100, carno, name, phone, bankname);
					} else {
						Toast.makeText (Tixian1Activity.this, "卡上余额不足，请充值！", Toast.LENGTH_SHORT)
								.show ();
					}
				}
			}
		}
		
		@Override
		public void onCancelPay() {
			// TODO Auto-generated method stub
			mDialogWidget.dismiss();
			mDialogWidget=null;
			Toast.makeText(getApplicationContext(), "交易已取消", Toast.LENGTH_SHORT).show();
			
		}
	}).getView();
}

private void tixiandata(String money, String cardno, String name,
		String phone, String carbank) {
	if (application.getDatas_load ()==null)return;
	AsyncHttpClient ahc = new AsyncHttpClient();
	PersistentCookieStore myCookieStore = new PersistentCookieStore(
			Tixian1Activity.this);
	ahc.setCookieStore(myCookieStore);
	RequestParams params = new RequestParams();
	params.put("g", "api");
	params.put("m", "ump");
	params.put("a", "withDraw");
	params.put("user_id", application.getDatas_load().getId());
	params.put("vipLevel", "2");//�û��ȼ�
	params.put("amount", money);
	params.put("recv_bank_acc_pro", "0");
	params.put("recv_account", cardno);
	params.put("recv_user_name", name);
	params.put("bank_brhname", carbank);
	params.put("mobile_no", phone);
	ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
		
		@Override
		public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						
						Log.e("提现", "提现" + response);
						try {
							String flag = response.getString("flag");
							String info = response.getString("info");
							if (flag.equals("success")) {
//								JSONObject jsonObject = response.getJSONObject("data");
//								JSONArray jsonArray = jsonObject.getJSONArray("data");
								Toast.makeText(Tixian1Activity.this, ""+info,
										Toast.LENGTH_SHORT).show();
								finish();
							}else{
								Toast.makeText(Tixian1Activity.this, ""+info,
										Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
		
		@Override
		public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
			super.onFailure(statusCode, headers, responseString, throwable);
			
			JSONObject json;
			try {
				json = new JSONObject(responseString);
				String flag = json.getString("flag");
				String info = json.getString("info");
				if(flag.equals("success")){
					Toast.makeText(Tixian1Activity.this, ""+info, Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(Tixian1Activity.this, "提现失败"+info, Toast.LENGTH_SHORT).show();
				}
				Log.e("string", "string"+info);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			Toast.makeText(TixianActivity.this, "���ʧ�ܣ�",
//					Toast.LENGTH_SHORT).show();
			Log.e("tianxian", "失败" + responseString);
		}

		@Override
		public void onStart() {
			// TODO Auto-generated method stub
			super.onStart();
			mTok.setClickable(false);
//			mRelati2.setVisibility(View.VISIBLE);
		}

		@Override
		public void onFinish() {
			super.onFinish();
			mTok.setClickable(true);
//			mRelati2.setVisibility(View.GONE);
		}
	});
}


}
